import React, { Component } from 'react';
import { Chart, Axis, Tooltip, Geom } from 'bizcharts';
import { Modal,Table } from 'antd';
import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
import ResizeObserver from 'resize-observer-polyfill';
import styles from './bar.less';

class FormationBar extends Component {
  state = {
    height: 0,
    autoHideXLabels: false,
    modalByFormation: false,
   	tableListDataByFormation: [
	    {unit_superior_unit:"乌兰察布市公安局", formation_type: "", unit_level:"市级", administrative_area:"乌兰察布市",approve_time: "2017-08-09",approve_unit: "乌兰察布市人民政府"},
			{unit_superior_unit:"呼和浩特市公安局", formation_type: "",unit_level:"市级", administrative_area:"呼和浩特市",approve_time: "2017-09-10",approve_unit: "呼和浩特市人民政府"},
			{unit_superior_unit:"包头市公安局", formation_type: "",unit_level:"市级", administrative_area:"包头市",approve_time: "2018-10-10",approve_unit: "包头市人民政府"},
			{unit_superior_unit:"乌海市公安局", formation_type: "", unit_level:"市级", administrative_area:"乌海市",approve_time: "2018-09-10",approve_unit: "乌海市人民政府"},
			{unit_superior_unit:"赤峰市公安局", formation_type: "",unit_level:"市级", administrative_area:"赤峰市",approve_time: "2018-05-10",approve_unit: "赤峰市人民政府"},
			{unit_superior_unit:"天津市公安局", formation_type: "",unit_level:"市级", administrative_area:"天津市",approve_time: "2016-09-10",approve_unit: "天津市人民政府"},
			{unit_superior_unit:"北京市公安局", formation_type: "",unit_level:"市级", administrative_area:"北京市",approve_time: "2015-09-10",approve_unit: "北京市人民政府"},
		]
  };
  
  columnsByFormation = [
		{
        title: '单位名称',
        dataIndex: 'unit_superior_unit',
        key: 'unit_superior_unit',
    },  {
        title: '编制类别',
        dataIndex: 'formation_type',
        key: 'formation_type',
    },  {
        title: '单位级别',
        dataIndex: 'unit_level',
        key: 'unit_level',
    },  {
        title: '单位所在政区',
        dataIndex: 'administrative_area',
        key: 'administrative_area',
    }, {
        title: '批准单位',
        dataIndex: 'approve_unit',
        key: 'approve_unit',
    }, {
        title: '批准时间',
        dataIndex: 'approve_time',
        key: 'approve_time',
    }
	];

  handleRoot = n => {
    this.root = n;
  };

  handleRef = n => {
    this.node = n;
  };

  resizeObserver() {
    const ro = new ResizeObserver(entries => {
      const { width, height } = entries[0].contentRect;
      this.resize();
      this.setState(preState => {
        if (preState.width !== width || preState.height !== height) {
          return {
            height,
          };
        }
        return null;
      });
    });
    if (this.root) {
      ro.observe(this.root);
    }
  }

  @Bind()
  @Debounce(400)
  resize() {
    if (!this.node) {
      return;
    }
    const canvasWidth = this.node.parentNode.clientWidth;
    const { data = [], autoLabel = true } = this.props;
    if (!autoLabel) {
      return;
    }
    const minWidth = data.length * 30;
    const { autoHideXLabels } = this.state;

    if (canvasWidth <= minWidth) {
      if (!autoHideXLabels) {
        this.setState({
          autoHideXLabels: true,
        });
      }
    } else if (autoHideXLabels) {
      this.setState({
        autoHideXLabels: false,
      });
    }
  }
	
  //编制弹窗
	handleModalByFormation = flag => {
		this.setState({
			modalByFormation: !!flag,
		});
	};

	
  render() {
    const {
      height: propsHeight,
      title,
      forceFit = true,
      data,
      color = 'rgba(24, 144, 255, 0.85)',
      padding,
      policeType,
    } = this.props;
    const { autoHideXLabels,modalByFormation,tableListDataByFormation } = this.state;

    const scale = {
      x: {
        type: 'cat',
      },
      y: {
        min: 0,
      },
    };

    const tooltip = [
      'x*y',
      (x, y) => ({
        name: x,
        value: y,
      }),
    ];
    const { height: stateHeight } = this.state;
    const height = propsHeight || stateHeight;
    return (
      <div className={styles.chart} style={{ height }} ref={this.handleRoot}>
     		<Modal
					width={800}
					centered
					destroyOnClose
					title="编制统计列表"
					visible={modalByFormation}
					onOk={()=> this.handleModalByFormation()}
					onCancel={()=> this.handleModalByFormation()}
					>
					<Table columns={this.columnsByFormation} dataSource={tableListDataByFormation}/>
				</Modal>
        <div ref={this.handleRef}>
          {title && <h4 style={{ marginBottom: 20 }}>{title}</h4>}
          <Chart
          	onPlotClick={ev => {
						    var point = {
						      x: ev.x,
						      y: ev.y
						    };
						    if(tableListDataByFormation) {
						    	tableListDataByFormation.map((val,index)=> {
						    		if(ev.data) {
						    			this.setState({
									    	modalByFormation: true
									    })
						    			val.formation_type = ev.data._origin.x
						    		}
						    		this.setState({
						    			tableListDataByFormation: [...tableListDataByFormation]
						    		})
						    	})
						    }
					  }}
            scale={scale}
            height={title ? height - 41 : height}
            forceFit={forceFit}
            data={data}
            padding={padding || 'auto'}
          >
            <Axis
              name="x"
              title={false}
              label={autoHideXLabels ? false : {}}
              tickLine={autoHideXLabels ? false : {}}
            />
            <Axis name="y" min={0} />
            <Tooltip showTitle={false} crosshairs={false} />
            <Geom type="interval" position="x*y" color={color} tooltip={tooltip} />
          </Chart>
        </div>
      </div>
    );
  }
}

export default FormationBar;
